package com.citycloud.ccuap.tc.admin.datagroup;


import com.alibaba.fastjson.JSONArray;
import com.citycloud.ccuap.commons.constants.GlobalOperateType;
import com.citycloud.ccuap.commons.db.LikeMatchMode;
import com.citycloud.ccuap.commons.db.SqlRestrictions;
import com.citycloud.ccuap.commons.json.CheckedTreeNode;
import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.openapi.controller.base.BaseController;
import com.citycloud.ccuap.commons.sys.code.ICodeService;
import com.citycloud.ccuap.commons.sys.entity.SysCode;
import com.citycloud.ccuap.commons.sys.entity.SysRole;
import com.citycloud.ccuap.commons.web.util.ResolveRequest;
import com.citycloud.ccuap.framework.mybatis.pagination.PageQuery;
import com.citycloud.ccuap.framework.util.StringUtil;
import com.citycloud.ccuap.tc.admin.datagrouplevel.IDatagroupLevelService;
import com.citycloud.ccuap.tc.admin.datagrouplevel.SysDatagroupLevelDTO;
import com.citycloud.ccuap.tc.admin.entity.SysDatagroup;
import com.citycloud.ccuap.tc.admin.entity.SysDatagroupLevel;
import com.citycloud.ccuap.tc.admin.sec.secuser.ISecUserCacheService;
import com.citycloud.ccuap.tc.yq.dimensionbustype.ISysDimensionBustypeService;
import com.citycloud.ccuap.tc.yq.dimensioncls.ISysDimensionClsService;
import com.citycloud.ccuap.tc.yq.dto.CheckedTreeNodeDTO;
import com.citycloud.ccuap.tc.yq.entity.MetaField;
import com.citycloud.ccuap.tc.yq.entity.SysDimensionBustype;
import com.citycloud.ccuap.tc.yq.metafield.IMetaFieldService;
import com.citycloud.ccuap.tc.yq.metafield.MetaFieldDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Api("数据组")
@RestController
@RequestMapping("/v1/yq/datagroup")
public class DatagroupController extends BaseController {
	
	@Autowired
	private IDatagroupService datagroupService;

	@Autowired
	private IMetaFieldService metaFieldService;

	@Autowired
	private ISysDimensionClsService secDimensionClsService;

	@Autowired
	private IDatagroupLevelService datagroupLevelService;

	@Autowired
	private ISysDimensionBustypeService secDimensionBustypeService;
	
	@Autowired
	private ISecUserCacheService secUserCacheService;
	
	@Autowired
	private ICodeService codeService;
	/**
	 * 默认构造函数
	 */
	public DatagroupController() {
	}
	
	/**
	 * 获取页面提交的对象
	 * 
	 * @return struts result
	 * @throws Exception
	 */
	private SysDatagroup getRequestDataObject(short operateType) throws Exception {
		SysDatagroup sysDatagroup = new SysDatagroup();
		ResolveRequest.fillData(request, sysDatagroup);

		if (GlobalOperateType.ADD == operateType) {
			sysDatagroup.setIsDeleted(0);
			sysDatagroup.setCreatedTime(new Date());
			sysDatagroup.setCreator(getCurrentUser().getUser().getUserNo());
		} else if (GlobalOperateType.EDIT == operateType) {
			sysDatagroup.setUpdator(getCurrentUser().getUser().getUserNo());
			sysDatagroup.setUpdatedTime(new Date());
		}

		return sysDatagroup;
	}
	
	@ApiOperation("根据数据组ID获取角色组")
	@RequestMapping(value = "/findSysRoleByDatagroupId", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findSysRoleById(){
		String objId = getStringParameter("datagroupId");
		List<SysRole> roles = datagroupService.findSysRoleByDatagroupId(objId);
		JSONData jsonData = new JSONData(roles);
		return jsonData;
	}

	@ApiOperation("根据主键获取对象")
	@RequestMapping(value = "/findById", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findById() throws Exception {
		String objId = getStringParameter("datagroupId");
		SysDatagroup sysDatagroup = datagroupService.findById(objId);
		JSONData jsonData = new JSONData(sysDatagroup);
		return jsonData;
	}

	@ApiOperation("新增对象")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JSONData add() throws Exception {
		SysDatagroup sysDatagroup = getRequestDataObject(GlobalOperateType.ADD);
		sysDatagroup = datagroupService.insert(sysDatagroup);
		secUserCacheService.buildDataGroupCacheById(sysDatagroup.getDatagroupId());
		JSONData jsonData = new JSONData("添加数据组成功！");
		return jsonData;
	}

	@ApiOperation("更新对象")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JSONData update() throws Exception {
		SysDatagroup sysDatagroup = getRequestDataObject(GlobalOperateType.EDIT);
		sysDatagroup = datagroupService.update(sysDatagroup);
		if(sysDatagroup.getIsEnable() == 1 ){
            secUserCacheService.buildDataGroupCacheById(sysDatagroup.getDatagroupId());
        } else {
            secUserCacheService.deleteDataGroupCacheById(sysDatagroup.getDatagroupId());
        }
		JSONData jsonData = new JSONData("修改数据组成功！");
		return jsonData;
	}

	@ApiOperation("条件配置")
	@RequestMapping(value = "/addCondition", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData addCondition() throws Exception {
		SysDimensionBustype sysDimensionBustype = new SysDimensionBustype();
		ResolveRequest.fillData(request, sysDimensionBustype);

		datagroupService.saveCondition(sysDimensionBustype);
		secUserCacheService.buildDataGroupCacheById(sysDimensionBustype.getDatagroupId());
		JSONData jsonData = new JSONData("条件配置成功！");
		return jsonData;
	}
	
	@ApiOperation("表权限列表")
	@RequestMapping(value = "/queryTableList", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData queryTableList() throws Exception {
		PageQuery<Map<String, Object>> pageQuery = ResolveRequest.getPageForParams(request);
		String datagroupId=getStringParameter("datagroupId");
		String dbId=getStringParameter("dbId");
		String firstCateId=getStringParameter("firstCateId");
		String secondCateId=getStringParameter("secondCateId");
		String tableName=getStringParameter("tableName");
		String tableEname=getStringParameter("tableEname");
		String sql ="SELECT d.OBJ_ID dbId, d.DB_NAME dbName, c1.OBJ_ID firstCatId, c1.CATE_NAME firstName, "
				+ "c2.OBJ_ID secondCatId, c2.CATE_NAME secondName, t.OBJ_ID tableId, t.TABLE_CNAME tableName, t.TABLE_ENAME tableEname "
				+ "FROM ap_t_meta_table_view t, ap_t_meta_table_cate_view c1, ap_t_meta_table_cate_view c2, "
				+ "cs_t_datasource_view d WHERE t.CATE_ID = c2.OBJ_ID AND c1.OBJ_ID = c2.PID AND "
				+ "t.DB_ID = d.OBJ_ID AND t.OBJ_ID NOT IN ( SELECT table_id FROM sys_dimension_table_config WHERE datagroup_id ='"+datagroupId+"')";
		sql+=SqlRestrictions.eq("t.DB_ID", dbId);
		sql+=SqlRestrictions.eq("c1.OBJ_ID", firstCateId);
		sql+=SqlRestrictions.eq("c2.OBJ_ID", secondCateId);
		sql+=SqlRestrictions.like("t.TABLE_CNAME", tableName, LikeMatchMode.BOTHADD);
		sql+=SqlRestrictions.like("t.TABLE_ENAME", tableEname, LikeMatchMode.BOTHADD);
		PageQuery<Map<String, Object>> pageData = datagroupService.selectPage(pageQuery, sql, true);
		
		JSONData jsonData = new JSONData(pageData);
		return jsonData;
	}
	
	@ApiOperation("已分配表字段权限列表")
	@RequestMapping(value = "/queryAssignedTableList", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData queryAssignedTableList() throws Exception {
		PageQuery<Map<String, Object>> pageQuery = ResolveRequest.getPageForParams(request);
		String datagroupId=getStringParameter("datagroupId");
		String dbId=getStringParameter("dbId");
		String firstCateId=getStringParameter("firstCateId");
		String secondCateId=getStringParameter("secondCateId");
		String tableName=getStringParameter("tableName");
		String tableEname=getStringParameter("tableEname");
		String sql ="SELECT d.OBJ_ID dbId, d.DB_NAME dbName, "
				+ "c1.OBJ_ID firstCatId, c1.CATE_NAME firstName, c2.OBJ_ID secondCatId, "
				+ "c2.CATE_NAME secondName, t.OBJ_ID tableId, t.TABLE_CNAME tableName, t.TABLE_ENAME tableEname,"
				+ "sc.table_config_id,sc.field_cnames as fieldName,sc.field_enames as fieldId FROM ap_t_meta_table_view t, ap_t_meta_table_cate_view c1,"
				+ " ap_t_meta_table_cate_view c2, cs_t_datasource_view d, sys_dimension_table_config sc "
				+ "WHERE t.CATE_ID = c2.OBJ_ID AND c1.OBJ_ID = c2.PID AND t.DB_ID = d.OBJ_ID AND "
				+ "t.OBJ_ID = sc.table_id AND sc.datagroup_id = '"+datagroupId+"' ";
		sql+=SqlRestrictions.eq("d.OBJ_ID", dbId);
		sql+=SqlRestrictions.eq("c1.OBJ_ID", firstCateId);
		sql+=SqlRestrictions.eq("c2.OBJ_ID", secondCateId);
		sql+=SqlRestrictions.like("t.TABLE_CNAME", tableName, LikeMatchMode.BOTHADD);
		sql+=SqlRestrictions.like("t.TABLE_ENAME", tableEname, LikeMatchMode.BOTHADD);
		PageQuery<Map<String, Object>> pageData = datagroupService.selectPage(pageQuery, sql, true);

		List<Map<String, Object>> records = pageData.getRecords();
		String totlefile="select count(*) from ap_t_meta_field_view as a where a.TABLE_ID='";
		int fieldSize = 0;
		for(Map<String, Object> map:records){
		    if(null == map.get("fieldName")){
		        fieldSize = 0;
            } else {
		        fieldSize = Arrays.asList(map.get("fieldName").toString().split(",")).size();
            }
			map.put("scale",fieldSize +"/"+metaFieldService.count(totlefile+map.get("tableId").toString()+"'"));
		}

		JSONData jsonData = new JSONData(pageData);
		return jsonData;
	}
	
	@ApiOperation("数据源列表")
	@RequestMapping(value = "/getDbList", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData getDbList() throws Exception {
		String sql ="select * from cs_t_datasource_view where 1=1";
		List<Map<String,Object>> list=datagroupService.selectList(sql, true);
		
		JSONData jsonData = new JSONData(list);
		return jsonData;
	}
	
	@ApiOperation("获取分类列表")
	@RequestMapping(value = "/getCateList", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData getCateList() throws Exception {
		String pid=getStringParameter("pid");
		String dsId=getStringParameter("dbId");
		String sql ="select * from ap_t_meta_table_cate_view where 1=1";
		if(StringUtils.isEmpty(pid)) {
			sql+=" and pid is null";
		}else {
			sql+=" and pid='"+pid+"'";
		}
		sql+=SqlRestrictions.eq("space_id", dsId);
		List<Map<String,Object>> list=datagroupService.selectList(sql, true);
		JSONData jsonData = new JSONData(list);
		return jsonData;
	}
	
	@ApiOperation("获取字段列表")
	@RequestMapping(value = "/getFieldByTableId", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData getFieldByTableId() throws Exception {
		PageQuery<MetaField> pageQuery = ResolveRequest.getPageForParams(request);
		MetaFieldDTO metaFieldDTO = new MetaFieldDTO();
		ResolveRequest.fillData(request, metaFieldDTO, "q_", true);
		
		PageQuery<MetaField> pageData = metaFieldService.selectPage(pageQuery, metaFieldDTO);
		JSONData jsonData = new JSONData(pageData);
		return jsonData;
	}
	
	@ApiOperation("查看详情")
	@RequestMapping(value = "/findInfoById", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findInfoById() throws Exception {
		Map<String,Object> map=new HashMap<>();
		String datagroupId=getStringParameter("datagroupId");
		SysDatagroup sysDatagroup=datagroupService.findById(datagroupId);
		String sql ="SELECT d.OBJ_ID dbId, d.DB_NAME dbName, "
				+ "c1.OBJ_ID firstCatId, c1.CATE_NAME firstName, c2.OBJ_ID secondCatId, "
				+ "c2.CATE_NAME secondName, t.OBJ_ID tableId, t.TABLE_CNAME tableName, "
				+ "sc.table_config_id ,sc.field_cnames as fieldName FROM ap_t_meta_table_view t, ap_t_meta_table_cate_view c1,"
				+ " ap_t_meta_table_cate_view c2, cs_t_datasource_view d, sys_dimension_table_config sc "
				+ "WHERE t.CATE_ID = c2.OBJ_ID AND c1.OBJ_ID = c2.PID AND t.DB_ID = d.OBJ_ID AND "
				+ "t.OBJ_ID = sc.table_id AND sc.datagroup_id = '"+datagroupId+"' ";
		List<Map<String,Object>> tableFieldList=datagroupService.selectList(sql);
		List<Map<String,Object>> bustypes = secDimensionBustypeService.getBusTypesByDatagroupId(datagroupId);
		bustypes.stream().filter(maps->Integer.valueOf(maps.get("specialType").toString())==3&&maps.get("bustypeDesc")!=null).forEach(maps->{
			List<Map<String,Object>> list=new ArrayList<>();
			list=secDimensionClsService.selectList("select GROUP_CONCAT(group_name) groupName from sys_group where group_id in("+maps.get("bustypeDesc")+")");
			if(list.size()>0) {
				maps.put("groupName", list.get(0).get("groupName"));
			}else {
				maps.put("groupName", "");
			}});
		bustypes.stream().filter(maps->maps.get("dicCodeType")!=null&&maps.get("dicCodeType")!="").forEach(maps->{
			List<SysCode> codeList =  codeService.findByCodeType(maps.get("dicCodeType").toString());
			maps.put("codeList", codeList);
		});
		map.put("sysDatagroup", sysDatagroup);
		map.put("tableFieldList", tableFieldList);
		map.put("bustypes", bustypes);
		JSONData jsonData = new JSONData(map);
		return jsonData;
	}
	
	@ApiOperation("增加表权限")
	@RequestMapping(value = "/addTableConfig", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData addTableConfig() throws Exception {
		String datagroupId=getStringParameter("datagroupId");
		List<SysDimensionTableConfigDTO> configList=JSONArray.parseArray(getStringParameter("tableConfigs"), SysDimensionTableConfigDTO.class);
		datagroupService.addTableConfig(datagroupId,configList);
		secUserCacheService.buildDataGroupCacheById(datagroupId);
		JSONData jsonData = new JSONData("增加表权限成功");
		return jsonData;
	}
	
	@ApiOperation("批量增加表权限")
	@RequestMapping(value = "/addBatchTableConfig", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData addBatchTableConfig() throws Exception {
		String datagroupId=getStringParameter("datagroupId");
		String dbId=getStringParameter("dbId");
		String firstCateId=getStringParameter("firstCateId");
		String secondCateId=getStringParameter("secondCateId");
		String tableName=getStringParameter("tableName");
		String sql =" SELECT uuid(),'"+datagroupId+"',d.OBJ_ID dbId, c1.OBJ_ID firstCatId, "
				+ " c2.OBJ_ID secondCatId,t.OBJ_ID tableId ,group_concat(f.ENAME) field_enames ,group_concat(f.CNAME) field_cnames "
				+ " FROM ap_t_meta_table_view t inner join ap_t_meta_table_cate_view c2 on "
				+" t.CATE_ID = c2.OBJ_ID AND t.OBJ_ID NOT IN ( SELECT table_id FROM sys_dimension_table_config WHERE datagroup_id ='"+datagroupId+"')  "
				+ SqlRestrictions.eq("t.DB_ID", dbId)
				+ SqlRestrictions.eq("c2.OBJ_ID", secondCateId)
				+ SqlRestrictions.like("t.TABLE_CNAME", tableName, LikeMatchMode.BOTHADD)
				+ " inner join ap_t_meta_table_cate_view c1 on c1.OBJ_ID = c2.PID "
				+ SqlRestrictions.eq("c1.OBJ_ID", firstCateId)
				+ "inner join cs_t_datasource_view d  on t.DB_ID = d.OBJ_ID left join  ap_t_meta_field_view f on f.TABLE_ID = t.obj_id group by d.OBJ_ID,c1.OBJ_ID,c2.OBJ_ID,t.OBJ_ID ";
		datagroupService.executeUpdate("insert  INTO sys_dimension_table_config (table_config_id,datagroup_id,ds_id,first_cat_id,second_cat_id,table_id,field_enames,field_cnames)  "+sql);
		secUserCacheService.buildDataGroupCacheById(datagroupId);
		JSONData jsonData = new JSONData("批量增加表权限成功");
		return jsonData;
	}
	
	@ApiOperation("增加字段配置")
	@RequestMapping(value = "/addFieldConfig",  method ={ RequestMethod.GET, RequestMethod.POST })
	public JSONData addFieldConfig() throws Exception {
		String datagroupId=getStringParameter("datagroupId");
		String tableConfigId=getStringParameter("tableConfigId");
		String fields=getStringParameter("fields");
		datagroupService.addFieldConfig(datagroupId,tableConfigId,fields);
		secUserCacheService.buildDataGroupCacheById(datagroupId);
		JSONData jsonData = new JSONData("字段配置成功");
		return jsonData;
	}
	
	@ApiOperation("删除表")
	@RequestMapping(value = "/deleteTable",  method ={ RequestMethod.GET, RequestMethod.POST })
	public JSONData deleteTable() throws Exception {
		String tableConfigIds=getStringParameter("tableConfigIds");
		String datagroupId =getStringParameter("datagroupId");
		datagroupService.deleteTable(tableConfigIds);
		secUserCacheService.buildDataGroupCacheById(datagroupId);
		JSONData jsonData = new JSONData("删除表成功");
		return jsonData;
	}
	
	@ApiOperation("删除条件")
	@RequestMapping(value = "/deleteBusType",  method ={ RequestMethod.GET, RequestMethod.POST })
	public JSONData deleteBusType() throws Exception {
		String objId=getStringParameter("objId");
		datagroupService.deleteBusType(objId);
		String datagroupId =getStringParameter("datagroupId");
		secUserCacheService.buildDataGroupCacheById(datagroupId);
		
		JSONData jsonData = new JSONData("删除条件成功");
		return jsonData;
	}
	
	@ApiOperation("删除对象")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JSONData delete() throws Exception {
		String objId = getStringParameter("datagroupId");
//		List<Map<String,Object>> sysActDatas = actDataService.selectList("SELECT t1.* FROM sys_datagroup t1 "
//				+ "INNER JOIN sys_project_dg_rel s ON t1.datagroup_id = s.datagroup_id AND "
//				+ "s.datagroup_id = '"+objId+"' LEFT JOIN sys_project_dg_role_rel t2 ON "
//						+ "t1.datagroup_id = t2.datagroup_id WHERE t1.is_enable = 1");
//		if(sysActDatas.size()>0) {
//			throw new ApplicationException("数据组已经分配，请先从站点角色中移除该数据组再删除。");
//		}
		datagroupService.deleteByObjId(objId);
		secUserCacheService.deleteDataGroupCacheById(objId);
		JSONData jsonData = new JSONData("删除数据组成功！");
		return jsonData;
	}

	@ApiOperation("删除多个对象")
	@RequestMapping(value = "/deleteList", method = RequestMethod.POST)
	public JSONData deleteList() throws Exception {
		String objIds = getStringParameter("datagroupIds");
		List<String> objIdList = StringUtil.strToStringList(objIds);

		datagroupService.deleteObjIdList(objIdList);
		objIdList.forEach(id ->{
			secUserCacheService.deleteDataGroupCacheById(id);
		});
		JSONData jsonData = new JSONData("删除数据组成功！");
		return jsonData;
	}

	@ApiOperation("根据条件查询对象")
	@RequestMapping(value = "/query", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData query() throws Exception {
		PageQuery<Map<String,Object>> pageQuery = ResolveRequest.getPageForParams(request);
		String name = getStringParameter("q_name");
        String sql="SELECT sd.*, GROUP_CONCAT(sp.project_name) name FROM sys_datagroup sd LEFT JOIN sys_project_dg_rel spd ON sd.datagroup_id = spd.datagroup_id LEFT JOIN sys_project sp ON spd.project_id = sp.project_id where 1=1";
		if(StringUtils.isNotBlank(name)) {
			sql+=" and (sd.datagroup_code like '%"+name+"%' or sd.datagroup_name like '%"+name+"%')";
		}
		sql+=" GROUP BY sd.datagroup_id order by sd.created_time desc";
        PageQuery<Map<String, Object>> pageData = datagroupService.selectPage(pageQuery, sql, true);

		JSONData jsonData = new JSONData(pageData);
		return jsonData;
	}

	@ApiOperation("查找树节点")
	@RequestMapping(value = "/findNextChildNodeTree", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findNextChildNodeTree() throws Exception {
		String groupId = getStringParameter("datagroupId");
		List<CheckedTreeNode> treeNodeList = null;
		treeNodeList = secDimensionClsService.findNextChildNodeTree(groupId);

		JSONData jsonData = new JSONData(treeNodeList);
		return jsonData;
	}

//	@ApiOperation("获取关联角色")
//	@RequestMapping(value = "/findLinkedRoles", method = { RequestMethod.GET, RequestMethod.POST })
//	public JSONData findLinkedRoles() throws Exception {
//		String groupId = getStringParameter("datagroupId");
//		SysProjectDgRoleRel sysActData = new SysProjectDgRoleRel();
//		sysActData.setDatagroupId(groupId);
//		List<SysProjectDgRoleRel> sysActDatas = actDataService.selectList(sysActData);
//		List<Long> list = sysActDatas.stream().map(t -> t.getRoleId()).collect(Collectors.toList());
//		JSONData jsonData = new JSONData(list);
//		return jsonData;
//	}

	@ApiOperation("根据数据组ID获取字段访问等级")
	@RequestMapping(value = "/findDatagroupLevelByDatagroupId", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findDatagroupLevelById(){
		String objId = getStringParameter("datagroupId");
		SysDatagroupLevelDTO level = new SysDatagroupLevelDTO();
		level.setObjId(objId);
		List<SysDatagroupLevel> levels = datagroupLevelService.findSysDatagroupLevelByDataGroupId(objId);
		List<Long> list = levels.stream().map(t -> t.getLevelId()).collect(Collectors.toList());
		JSONData jsonData = new JSONData(list);
		return jsonData;
	}

	@ApiOperation("根据数据组ID特定（机构）业务对象")
	@RequestMapping(value = "/findDimensionBustypeById", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findDimensionBustypeById(){
		String objId = getStringParameter("datagroupId");
		List<Map<String,Object>> bustypes = secDimensionBustypeService.getBusTypesByDatagroupId(objId);
		bustypes.stream().filter(map->Integer.valueOf(map.get("specialType").toString())==3&&map.get("bustypeDesc")!=null).forEach(map->{
			List<Map<String,Object>> list=new ArrayList<>();
			list=secDimensionClsService.selectList("select GROUP_CONCAT(group_name) groupName from sys_group where group_id in("+map.get("bustypeDesc")+")");
			if(list.size()>0) {
				map.put("groupName", list.get(0).get("groupName"));
			}else {
				map.put("groupName", "");
			}});
		bustypes.stream().filter(map->map.get("dicCodeType")!=null&&map.get("dicCodeType")!="").forEach(map->{
			List<SysCode> codeList =  codeService.findByCodeType(map.get("dicCodeType").toString());
			map.put("codeList", codeList);
		});
		JSONData jsonData = new JSONData(bustypes);
		return jsonData;
	}

	@ApiOperation("根据业务对象ID获取部分树")
	@RequestMapping(value = "/findBusiNextChildNodeTree", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findBusiNextChildNodeTree(){
		String objId = getStringParameter("objId");
		SysDimensionBustype secDimensionBustype =  secDimensionBustypeService.findById(objId);
		List<String> objIdList = new ArrayList<String>();
		if(null != secDimensionBustype && StringUtils.isNotEmpty(secDimensionBustype.getBustypeDesc())){
			objIdList =  StringUtil.strToStringList(secDimensionBustype.getBustypeDesc());
		}
		List<CheckedTreeNodeDTO> treeNodeList = datagroupService.findBusiNextChildNodeTree(objIdList,null,null);
		JSONData jsonData = new JSONData(treeNodeList);
		return jsonData;
	}

	@ApiOperation("根据业务对象ID获取选择分类、表、字段")
	@RequestMapping(value = "/findLinkedDimensions", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findLinkedDimensions(){
		String objId = getStringParameter("datagroupId");

		JSONData jsonData = new JSONData(secDimensionClsService.getLinkedDimensions(objId));
		return jsonData;
	}
	
	@ApiOperation("根据机构编号获取机构名称")
	@RequestMapping(value = "/getGroupNameByIds", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData getGroupNameByIds(){
		String groupIds = getStringParameter("groupIds");
		List<Map<String,Object>> list=new ArrayList<>();
		list=secDimensionClsService.selectList("select GROUP_CONCAT(group_name) groupName from sys_group where group_id in("+groupIds+")");
		if(list.size()>0) {
			JSONData jsonData = new JSONData(list.get(0));
			return jsonData;
		}else {
			JSONData jsonData = new JSONData();
			return jsonData;
		}
	}
}